package com.sws.worktive;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sws.worktive.task.ServerDownload;
import com.sws.worktive.task.ServerResponse;
import com.sws.worktive.util.Constant;
import com.sws.worktive.util.WT_Prefrences;

public class SigInFragment extends BaseFragment implements OnClickListener,
		ServerResponse {

	LinearLayout ll_back;
	EditText edt_phno;
	EditText edt_password;
	Button btn_signin;
	WT_Prefrences wtprefences;
	List<NameValuePair> nameValuePairs;
	
	//======================== API ========================
	public static SigInFragment newInstance(
			FragmentManager fragmentManager,
			String fragmentTag){
		SigInFragment signInFragment = (SigInFragment) fragmentManager.findFragmentByTag(fragmentTag);
		if (signInFragment == null) {
			signInFragment = new SigInFragment();
			return signInFragment;
		}
		return null;
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public android.view.View onCreateView(android.view.LayoutInflater inflater,
			android.view.ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.signin_screen, container, false);
		TextView txtHeader = (TextView) view.findViewById(R.id.screen_title);
		txtHeader.setText(getResources().getString(R.string.sign_in));
		edt_password = (EditText) view.findViewById(R.id.edt_password);
		edt_phno = (EditText) view.findViewById(R.id.edt_phno);
		wtprefences=new WT_Prefrences(getActivity());
		ll_back = (LinearLayout) view.findViewById(R.id.layout_back);
		ll_back.setOnClickListener(this);
		btn_signin = (Button) view.findViewById(R.id.btn_signin);
		btn_signin.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_back:
			FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
			fragmentManager.popBackStack();
			break;
		case R.id.btn_signin:
			if (edt_phno.getText().toString().trim().length() == 0
					|| edt_password.getText().toString().trim().length() == 0) {
				Toast.makeText(getActivity(),
						getString(R.string.emtpypnumberpassword),
						Toast.LENGTH_SHORT).show();
			} else {

				nameValuePairs = new ArrayList<NameValuePair>();
				nameValuePairs.add(new BasicNameValuePair("phoneNumber",
						edt_phno.getText().toString()));
				nameValuePairs.add(new BasicNameValuePair("password",
						edt_password.getText().toString().trim()));
				nameValuePairs.add(new BasicNameValuePair("devapipassword",
						Constant.PASSWORD_VALUE));

				((DrawerFragment) getActivity()).mProgressDialog = ProgressDialog
						.show(getActivity(), "", getActivity().getResources()
								.getString(R.string.loginloadingmessage));
				ServerDownload serverDownload = new ServerDownload(
						getActivity(), SigInFragment.this,
						Constant.SIGNIN_FRAGMENT);
				serverDownload.execute(nameValuePairs);
			}

			break;
		}
	}

	@Override
	public void httpResponse(JSONObject jsonObject, String requestTag) {

		try {
			if (jsonObject != null && jsonObject instanceof JSONObject) {
				int status = jsonObject.getInt("status");
				if (status == 1) {
					if(((DrawerFragment) getActivity()).mProgressDialog!=null)
						((DrawerFragment) getActivity()).mProgressDialog.dismiss();
					
					Toast.makeText(getActivity(),
							jsonObject.getString("message"), Toast.LENGTH_SHORT)
							.show();
					JSONObject userObj = jsonObject.getJSONObject("user");
					if (userObj != null && userObj instanceof JSONObject) {
						int phoneNumber = userObj.getInt("phoneNumber");
						String token = userObj.getString("token");
						wtprefences.setToken(token);
						wtprefences.setPhonenumber(phoneNumber);
					}
					getActivity().getSupportFragmentManager().popBackStack();
					((DrawerFragment)getActivity()).addFragment(0);
				}else{
					if(((DrawerFragment) getActivity()).mProgressDialog!=null)
						((DrawerFragment) getActivity()).mProgressDialog.dismiss();
					
					Toast.makeText(getActivity(),
							jsonObject.getString("message"), Toast.LENGTH_SHORT)
							.show();
				}
			}
		} catch (Exception ex) {

		}

	};

}
